package com.young.ysepay.service.impl;

import com.alibaba.fastjson.JSON;
import com.young.ysepay.dto.YsPayDTO;
import com.young.ysepay.service.YsBuildRequestParaService;
import com.young.ysepay.service.YsBusinessPramService;
import com.young.ysepay.service.YsSplitRequestService;
import com.young.ysepay.service.YsVerifyJsonSignService;
import com.young.ysepay.utils.DateFormatHelper;
import com.young.ysepay.utils.Https;
import com.young.ysepay.utils.JSONUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: WANGNING
 * @Email wyn20140226@aliyun.com
 * @Date: 2019/6/4 18:40
 * @Des
 */
@Service
public class YsSplitRequestServiceImpl implements YsSplitRequestService {
    public static final Logger logger = LoggerFactory.getLogger(YsSplitRequestServiceImpl.class);
    @Autowired
    private YsBusinessPramService ysBusinessPramService;
    @Autowired
    private YsBuildRequestParaService ysBuildRequestParaService;
    @Autowired
    private YsVerifyJsonSignService ysVerifyJsonSignService;
    @Override
    public String splitRequest(Map<String, Object> mapParams){
        logger.info("分账查询参数"+JSONUtils.objectToJson(mapParams));
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String str = "";
        //获取配置数据
        YsPayDTO ysPayDTO=ysBusinessPramService.ysBusinessPram();
        Object temp = mapParams.get("biz_content");
        String bizContent = temp != null ? temp.toString() : null;
        Map<String, Object> jsonMap = JSONUtils.jsonToMap(bizContent);
        temp = jsonMap.get("out_trade_no");
        String out_trade_no = temp != null ? temp.toString() : null;
        //封装参数
        Map<String, String> param = new HashMap<>();
        param.put("method", "ysepay.single.division.online.query");
        param.put("partner_id", ysPayDTO.getPARTNER_ID());
        param.put("timestamp", DateFormatHelper.getNowTimeStr());
        param.put("charset", ysPayDTO.getDEFAULT_CHARSET());
        param.put("sign_type", ysPayDTO.getSIGN_ALGORITHM());
        param.put("version", ysPayDTO.getVERSION());
        param.put("biz_content", bizContent);

        try {
            //签名
            Map<String, String> sPara = ysBuildRequestParaService.buildRequestPara(param);
            String returnString = Https.httpsSend(ysPayDTO.getYSEPAY_GATEWAY_URL_DSF(), sPara);
            resultMap = (Map) JSON.parse(returnString);
            Object obj = resultMap.get("ysepay_single_division_online_query_response");
            boolean verifyResult = false;
            if (obj != null) {
                int yscount = returnString.indexOf("ysepay_single_division_online_query_response");
                str = returnString.substring(yscount);
                str = str.replace("ysepay_single_division_online_query_response", "");
                str = str.substring(2, str.length() - 1);
                verifyResult = ysVerifyJsonSignService.verifyJsonSign(resultMap.get("sign").toString(), str, "utf-8");
                logger.error("银盛分账查询接口验签："+verifyResult);
                logger.error("银盛分账查询回执信息"+str);
                return str;
            }
        } catch (IOException e) {
            logger.error("银盛分账查询接口"+e.getMessage());
            e.printStackTrace();
        } catch (IllegalAccessException e1) {
            logger.error("银盛分账查询接口"+e1.getMessage());
            e1.printStackTrace();
        } catch (Exception e) {
            logger.error("银盛分账查询接口"+e.getMessage());
            e.printStackTrace();
        } finally {
            logger.info("订单号:{}优店平台调用银盛分账查询回执：{}",out_trade_no,str);
            return str;
        }
    }
}
